package com.kzj.mls.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ab.util.AbSharedUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzj.mls.R;
import com.kzj.mls.activity.GBaseActivity;
import com.kzj.mls.activity.LoginActivity;
import com.kzj.mls.activity.MoneyActivity;
import com.kzj.mls.beans.CloseActivityBean;
import com.kzj.mls.beans.MoneyControlBean;
import com.kzj.mls.beans.OrderBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListAdapter;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.OnButtonPressListener;
import com.kzj.mls.recyclerCard.basic.SuperSwipeRefresh;
import com.kzj.mls.recyclerCard.card.Card;
import com.kzj.mls.recyclerCard.card.other.GoToLoginCard;
import com.kzj.mls.recyclerCard.card.other.LoadingCard;
import com.kzj.mls.recyclerCard.card.other.MoneyCalculateCard;
import com.kzj.mls.recyclerCard.card.other.NotNetCard;
import com.kzj.mls.recyclerCard.card.other.NotOrderCard;
import com.kzj.mls.recyclerCard.card.other.OrderCard;
import com.kzj.mls.recyclerCard.cardView.ServiceOrderCardView;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.kzj.mls.widget.dialog.SweetDialogManager;
import com.kzj.mls.wxapi.WXPayEntryActivity;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/10/9.
 */
public class AllMoneyCalculateFragment extends BaseFragment implements SuperSwipeRefresh.OnRefreshReceyerListener, SuperSwipeRefresh.OnLoadMoreListener {
    private MaterialListView materialListView;
    private String status = "0";
    private View holderView;
    private SuperSwipeRefresh superSwipeRefresh;



    public  AllMoneyCalculateFragment(String status)
    {
        this.status=status;
    }
    public  AllMoneyCalculateFragment()
    {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.template_superswiperefresh_materlistview, container, false);
            findViewByIds();
            initViews();
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        return holderView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void findViewByIds() {
        materialListView = (MaterialListView) holderView.findViewById(R.id.material_listview);
        superSwipeRefresh = (SuperSwipeRefresh) holderView.findViewById(R.id.superSwipeRefresh);
        superSwipeRefresh.setPageSize(10);
        superSwipeRefresh.setView(getActivity(), materialListView);
        superSwipeRefresh.setEnableLoadMore(false);
        superSwipeRefresh.setOnRefreshReceyerListener(this);
     // superSwipeRefresh.setOnLoadMoreListener(this);

    }

    public void initViews() {
        TaskGetMoneyControl(status);
    }

    public void refreshCard(MoneyControlBean moneyControlBean) {
    /*    if (materialListView.getAdapter().getItemCount()==0) {
            MoneyCalculateCard moneyCalculateCard = new MoneyCalculateCard(getActivity());
            moneyCalculateCard.setMoneyControlBean(moneyControlBean);
            materialListView.add(moneyCalculateCard);
        }else
        {
            MoneyCalculateCard moneyCalculateCard= (MoneyCalculateCard) ((MaterialListAdapter)materialListView.getAdapter()).getCard(0);
            moneyCalculateCard.setMoneyControlBean(moneyControlBean);
            materialListView.getAdapter().notifyDataSetChanged();
        }*/
        materialListView.clear();
        for (int i=0;i<moneyControlBean.getOrderList().size();i++)
        {
            MoneyCalculateCard moneyCalculateCard = new MoneyCalculateCard(getActivity());
            moneyCalculateCard.setOrderListEntity(moneyControlBean.getOrderList().get(i));
            materialListView.add(moneyCalculateCard);
        }


    }

    @Override
    public void fillArrayInList() {

    }


    @Override
    public void loadMore(int itemsCount, int maxLastVisiblePosition) {

    }

    @Override
    public void Refresh(MaterialListView recyclerView) {
        TaskGetMoneyControl(status);
    }


    private void TaskGetMoneyControl(final String status) {
        RequestServerManager.getInstance().handleMethod(null, null, superSwipeRefresh, false,
                Constant.getRootUrl() + Constant.Action.Action_FundManage, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_FundManage,status), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        final MoneyControlBean moneyControlBean = JSON.parseObject(JSON.toJSONString(result.getRetval()), MoneyControlBean.class);
                        if (status.equals(MoneyActivity.qb))
                            EventBus.getDefault().post(moneyControlBean);
                        refreshCard(moneyControlBean);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
}
